package com.caotv.wkreport.dbutils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;



import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.caotv.wkreport.dao.*;
import com.caotv.wkreport.utils.LoggingUtils;
import org.hibernate.cfg.Configuration;
import org.hibernate.Hibernate;

public class BookingRoomTableViewTool {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public static BookingRoomTableViewTool getInstance() {
		BookingRoomTableViewTool bookingRoomTableViewTool = new BookingRoomTableViewTool();
		bookingRoomTableViewTool.sessionFactory = HibernateUtil.getSessionFactory();
		return bookingRoomTableViewTool;
	}

	public List<Bookingroom> getBookingRoomByID(Integer id) {
		List<Bookingroom> list = new ArrayList<Bookingroom>();
		try {
			Session session = sessionFactory.openSession();
			
//			Bookingroom room = (Bookingroom) session.get(Bookingroom.class, id);
		     // query to issue
	        String query = "select bookingroom from Bookingroom " + "where bookingroom.roomId=:id";

	    
	        // init
	        Configuration cfg = new Configuration().addClass(Bookingroom.class);

	        SessionFactory sf = cfg.buildSessionFactory();

	        // open session
	        Session sess = sf.openSession();

	        // search and return
	       // list = sess.find(query, id, Hibernate.STRING);

	        if (list.size() == 0) {
	            System.out.println("No Order having id " + id);
	            System.exit(0);
	        }
	        
	        
			session.close();
			return list;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get room by id");
			ex.printStackTrace();
			return null;
		}		
	}
		
	
	
	public List<Rooms> getAllRooms() {
		List<Rooms> list = new ArrayList<Rooms>();
		try {
			Session session = sessionFactory.openSession();
			
			list = session
					.createCriteria(Rooms.class)
					.list();
			session.close();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get all rooms");
			ex.printStackTrace();
		}
		return list;
	}
}
